/*
 * Copyright (c) 2012 Johan Fylling
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 * THE SOFTWARE.
 */

part of engine;


/**
 * Axis-Alligned Bounding Box.
 */
class AABB {
  Vector2 min;
  Vector2 max;
  
  AABB () {
    this.min = new Vector2(0,0);
    this.max = new Vector2(0,0);
  }
  
  num get width => this.max.x - this.min.x;
  
  num get height => this.max.y - this.min.y;
}

/**
 * Collidable.
 */
abstract class Collidable {
  /** Returns the Bounding Sphere radius. */
  num get boundingSphereRadius;
  
  /** Appends the provided list with the collidable foci. */
  void getFoci (List<Vector2> foci, DynamicBody body);
  
  /** Appends the provided list with the collidable axes. */
  void getAxes (List<Vector2> axes, List<Vector2> foci, DynamicBody body);
  
  /** Creates an [Interval] representing the projection of this [Collidable] on the provided axis. */
  Interval project (Vector2 axis, DynamicBody body);
  
  /** Returns the [ContactFeature] farthest down the provided direction. */
  ContactFeature getFarthestFeature (DynamicBody body, Vector2 direction);
  
  /** Returns the Bounding Sphere Offset. Defaults to (0,0). */
  Vector2 get boundingSphereOffset => new Vector2(0,0);
  
  /** Returns the [AABB]. Defaults to contain the Bounding Sphere. */
  void getAABB (AABB aabb, DynamicBody body) {
    Vector2 center = body.position + boundingSphereOffset;
    num radius = boundingSphereRadius;
    
    aabb.min.x = center.x - radius;
    aabb.min.y = center.y - radius;
    aabb.max.x = center.x + radius;
    aabb.max.y = center.y + radius;
  }
  
  bool raycast (Vector2 from, Vector2 to, DynamicBody body);
  
  /** Generates a random point within the [Collidable]. */
  Vector2 getRandomPoint (DynamicBody body, Math.Random random);
  
  toJson () {
    return {
      "type": "unknown_collidable"
    };
  }
}
